#include <animation/NumericAnimationTrack.h>

namespace ne
{
    NumericAnimationTrack::NumericAnimationTrack(Animation *parent, const uint16_t handle)
        : AnimationTrack(parent, handle),
          mpTargetNumeric(0)
    {
    }

    NumericAnimationTrack::NumericAnimationTrack(Animation *parent, const uint16_t handle, real *number)
        : AnimationTrack(parent, handle),
          mpTargetNumeric(number)
    {
    }

    NumericKeyFrame* NumericAnimationTrack::createNumericKeyFrame(const real time)
    {
        return dynamic_cast<NumericKeyFrame*>(createKeyFrame(time));
    }

    void NumericAnimationTrack::setAssociatedNumeric(real *number)
    {
        mpTargetNumeric = number;
    }

    real* NumericAnimationTrack::getAssociatedNumeric() const
    {
        return mpTargetNumeric;
    }

    NumericKeyFrame* NumericAnimationTrack::getNumericKeyFrame(const size_t index) const
    {
        return dynamic_cast<NumericKeyFrame*>(getKeyFrame(index));
    }

    void NumericAnimationTrack::getKeyFrameAtTime(const real time, KeyFrame &keyFrame) const
    {
        _sort();

        /*

        // Binary Search
        KeyFrame *baseBeginFrame = 0;
        KeyFrame *baseEndFrame = 0;

        if (getKeyFramesCount())
        {
            _binarySearchKeyFramesAtTime(0, getNumKeyFrames()-1, time, &baseBeginFrame, &baseEndFrame);
        }

        NumericKeyFrame *beginKeyFrame = dynamic_cast<NumericKeyFrame*>(baseBeginFrame);
        NumericKeyFrame *endKeyFrame = dynamic_cast<NumericKeyFrame*>(baseEndFrame);

        NumericKeyFrame *midKeyFrame = dynamic_cast<NumericKeyFrame*>(&keyFrame);
        if (beginKeyFrame == 0)
        {
            if (endKeyFrame == 0)
            {
                //midKeyFrame->setTranslate(Vector3::ZERO);
            }
            else
            {
                real u = time / endKeyFrame->getTime();
                //Vector3 translate = endKeyFrame->getTranslate() * u;
                //midKeyFrame->setTranslate(translate);
            }
        }
        else
        {
            if (endKeyFrame == 0)
            {
                //midKeyFrame->setTranslate(beginKeyFrame->getTranslate());
            }
            else
            {
                real u = (time - beginKeyFrame->getTime()) / (endKeyFrame->getTime() - beginKeyFrame->getTime());
                //Vector3 translate = beginKeyFrame->getTranslate() + (endKeyFrame->getTranslate() - beginKeyFrame->getTranslate()) * u;
                //midKeyFrame->setTranslate(translate);
            }
        }

        */
    }

    void NumericAnimationTrack::getNumericKeyFrameAtTime(const real time, NumericKeyFrame &keyFrame) const
    {
        getKeyFrameAtTime(time, keyFrame);
    }

    void NumericAnimationTrack::apply(const real time, const real weight, const real scaleU)
    {
        applyToNumeric(mpTargetNumeric, time, weight, scaleU);
    }

    void NumericAnimationTrack::applyToNumeric(real *number, const real time, const real weight, const real scaleU)
    {
    }

    bool NumericAnimationTrack::hasNonZeroKeyFrames() const
    {
        return false;
    }

    void NumericAnimationTrack::optimise()
    {
    }

    void NumericAnimationTrack::_keyFrameDataChanged() const
    {
    }

    NumericAnimationTrack::~NumericAnimationTrack()
    {
    }

    KeyFrame* NumericAnimationTrack::_createKeyFrameImpl(const real time)
    {
        return new NumericKeyFrame(this, time);
    }
}
